<?php
	namespace Home\Model;
	use Think\Model;
	/**
	 * 文章表模型类
	 */
	class ArticleModel extends Model {
		/**
		 * 获取推荐的文章文章，前四个
		 */	
		public function getRecommendArticle() {
			/**
			 * 这里暂时是获取前四个
			 */
			$sql = "select article.id,thumb,title,ar_desc,hits,cre_time,author,cate_name from article join cate on article.cate_pid=cate.id where article.is_delete=0 limit 0,4";
			return $this->query($sql);
			//return $this->where('is_delete=0')->limit(0,4)->select();
			//return $this->field('article.id,thumb,title,ar_desc,hits,cre_time,cate_name')->join('left cate on article.cate_pid=cate.id')->where('article.is_delete=0')->limit('0,4')->select();
		}
		/**
		 * 根据id获取文章信息
		 */
		public function getArticleById($id) {
			$sql = "select article.*,cate_name from article left join cate on article.cate_pid=cate.id where article.is_delete=0 and article.id=$id";
			return $this->query($sql)[0];
			//return $this->field('article.*,cate_name')->join('left join cate on article.cate_pid=cate.id')->where('article.is_delete=0')->find($id);
		}
		
		/**
		 * 获取最受欢迎文章
		 */
		public function getLikeArt() {
			return $this->field('id,title,cre_time,thumb')->limit(0,4)->where('is_delete=0')->order('hits')->select();
		}
		
		/**
		 * 根据分类id获取文章
		 */
		public function getArticleByCateId($cate_id,$start,$offset) {
			return $this->where('is_delete=0 and cate_pid='.$cate_id)->limit("$start,$offset")->select();
		}

		/**
		 * 根据分类id获取该分类下的记录数
		 */
		public function getCountByCateId($cateId) {
			return $this->where('is_delete=0 and cate_pid='.$cateId)->count();
		}
		
		/**
		 * 更新文章点击数
		 */
		public function updateHits($id) {
			// 获取当前点击数
			$data['hits'] = $this->field('hits')->find($id)['hits'];
			$data['hits']++;
			$data['id'] = $id;
			$this->save($data);
		}
		
		/**
		 * 更新留言数
		 */
		public function updateAppraise($id) {
			// 获取当前点击数
			$data['appraise_num'] = $this->field('appraise_num')->find($id)['appraise_num'];
			$data['appraise_num']++;
			$data['id'] = $id;
			$this->save($data);
		}
	}